package chat.presentation;

import chat.message.ChatMessage;
import chat.message.ChatMessageManager;
import chat.message.MessageHistoryManager;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ViewScoped;
import java.util.Date;
import java.util.List;

@ManagedBean
@ViewScoped
public class ChatBean {
    private String message;
    private String from;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }


    public void postMessage() {
        if (message != null && !message.trim().isEmpty()) {
            ChatMessageManager.addMessage(new ChatMessage(from, message, new Date()));
        }
        message = null;
    }

    public List<ChatMessage> getHistory() {
        return MessageHistoryManager.getRecentMessages();
    }
}
